<!DOCTYPE html>
<html>
<head>
    <title>编辑病害数据</title>
    {% include 'admin/common/header.html' %}
</head>
<body>
<form class="layui-form" lay-filter="pest-edit">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">病害名称</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ wiki_info.disease_name }}" name="disease_name" lay-verify="title" autocomplete="off" placeholder="请输入病害名称"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">危害</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ wiki_info.disease_harm  }}" name="disease_harm" lay-verify="title" autocomplete="off" placeholder="请输入危害"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">症状</label>
                    <div class="layui-input-block">
                        <textarea id="symptom" style="height: 180px" name="disease_symptom" required lay-verify="required" autocomplete="off" placeholder="请输入症状" class="layui-textarea">{{ wiki_info.disease_symptom }}</textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">防治方法</label>
                    <div class="layui-input-block">
                        <textarea id="prevention" style="height: 180px" name="disease_prevention" required lay-verify="required" placeholder="请输入防治方法" class="layui-textarea">{{ wiki_info.disease_prevention }}</textarea>
                    </div>
                </div>

                <!-- 参考上传文件页面 -->
                <div class="layui-form-item">
                    <label class="layui-form-label">病害图片</label>
                        <button type="button" class="layui-btn layui-btn-normal" id="logo-img">选择文件</button>
                        <button type="button" class="layui-btn" id="logo-upload-button">开始上传</button>
                    <div class="layui-upload-list">
                      <img class="layui-upload-img" src="{{ wiki_info.disease_img }}" id="pestimg" style="width: 92px;height: 92px;margin-left: 110px">
                     </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">说明</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ wiki_info.disease_explain }}" name="disease_explain" lay-verify="title" autocomplete="off" placeholder="请输入说明"
                               class="layui-input">
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="pest-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'admin/common/footer.html' %}
<script>
  layui.use(['form', 'jquery', 'element', 'upload'], function () {
      let form = layui.form
      let $ = layui.jquery
      var element = layui.element;
      var upload = layui.upload;
      var pestFile = "{{ wiki_info.disease_img }}"



      upload.render({
        elem: '#logo-img'
            , url: "/pest_search/img"
            , auto: false
            , exts: 'jpg|png|gif|bmp|jpeg'
            , size: 5000
            , bindAction: '#logo-upload-button'
            ,done: function (res) {
                if (res.success) {
                    layer.msg(res.msg, {icon: 1, time: 500}, function () {
                        pestFile = res.data.src
                        console.log("上传之后的路径:", pestFile)

                    });
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }
        });

        form.on('submit(pest-update)', function (data) {
          //  权限相关,还没用到
          let roleIds = ''
          $('input[type=checkbox]:checked').each(function () {
            roleIds += $(this).val() + ','
          })
          roleIds = roleIds.substr(0, roleIds.length - 1)
          data.field.roleIds = roleIds
            data.field.disease_num = "{{ wiki_info.disease_num }}"
          data.field.disease_img = pestFile
          data.field.disease_access = {{ wiki_info.disease_access }}+1

          console.log("表单里的数据", data)

          $.ajax({
            url: '/pest_search/update',
            data: JSON.stringify(data.field),
            dataType: 'json',
            contentType: 'application/json',
            type: 'put',
            success: function (result) {
              if (result.success) {
                layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                  parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                  parent.layui.table.reload('wiki-table')
                })
              } else {
                layer.msg(result.msg, { icon: 2, time: 1000 })
              }
            }
          })
          return false
        });


  })
</script>

</body>
</html>